package com.wk.masterNode;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import org.junit.experimental.theories.FromDataPoints;

import com.wk.database.DB_Mysql;
import com.wk.job.Job;
import com.wk.job.Task;

/***
 * Creator 类以轮询（触发）方式访问数据库，时间间隔为5s
 * 
 * 1. Creator 从数据库读取状态存放到题 tasksQueue同步队列中。 2. 由分析器analizeTask(task)
 * 分析task生成一个或者多个job，并放入jobsQueue中。
 * 
 * @author wkhua
 *
 */
public class Creator implements Runnable {

	private LinkedBlockingQueue<Task> tasksQueue = new LinkedBlockingQueue<>();

	private LinkedBlockingQueue<Job> jobsQueue = null;
	ConcurrentHashMap<Integer,Task> tasks = null;

	DB_Mysql db_Mysql = new DB_Mysql();
	private boolean isWorking = true;

	public Creator(LinkedBlockingQueue<Job> jobsQueue,ConcurrentHashMap<Integer,Task> tasks) {
		this.tasks = tasks;
		this.jobsQueue = jobsQueue;
	}

	@Override
	public void run() {
		int counts = 0;
		int MAX = 10;
		long seconds = 2;
		while (isWorking) {
			SimpleDateFormat timeFormate = new SimpleDateFormat("yyyy-MM-dd");
			String now = timeFormate.format(new Date());
			try {
				Thread.sleep(seconds * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// 从数据库向TaskQueue添加任务
			getFromDB(tasksQueue, now);
			analizeTask(tasksQueue,tasks);
			
			if (counts < MAX) {
				counts++;
			} else {
				counts = 0;
			}

		}
	}

	/***
	 * 分析Task任务，生成jobs并添加到jobsQueue中
	 * 
	 * @param resultsSet
	 */
	private void analizeTask(LinkedBlockingQueue<Task> tasklist,ConcurrentHashMap<Integer,Task> tasks) {
	
			while( !tasklist.isEmpty()){
				Task task = tasklist.poll();
					if(task !=null){
						tasks.put(task.getTaskId(), task);//记录所有得到的task
						ArrayList<Job> jobs = task.generateJobs();//由task自己分析然后返回jobs
						for(Job job :jobs){
							boolean foo =jobsQueue.add(job);
							System.out.println("foo: "+foo+" jobsQueue.size() "+jobsQueue.size());
						}
					}
			}
	//		System.out.println("分析Task完成");
		
	}

	

	/***
	 * 查询数据库得到所有任务的结果集并把结果集交给ananlizeTask进行解析
	 * 
	 * @param now
	 */
	private void getFromDB(LinkedBlockingQueue<Task> tasksQueue,String now) {
		String findTaskSql = "SELECT id,production_plan_name,product_type,region_range FROM t_business_production_plan_management WHERE production_status='待生产' AND production_begin_time<='"
				+ now + "'"; // 查询当前时间之前的所有状态为带生产的数据;
		db_Mysql.getTasks(tasksQueue,findTaskSql);// 查询数据库
		System.out .println("tasksQueue.size(): "+tasksQueue.size());//tasksQueue队列长度
		return ;
			
	}

	/***
	 * 从配置文件中获取所有任务，并把结果集交给analizeTask进行解析
	 * 
	 * @param now
	 */
	private void getFromFile(String now) {

		/*
		 * int taskId = resultsSet.getInt("id"); String production_plan_name =
		 * resultsSet.getString("production_plan_name"); String product_type =
		 * resultsSet.getString("product_type"); String region_code =
		 * resultsSet.getString("region_range");
		 * 
		 * Task task = new Task(taskId, production_plan_name, product_type,
		 * region_code);//创建一个Taskint taskId = resultsSet.getInt("id"); String
		 * production_plan_name = resultsSet.getString("production_plan_name");
		 * String product_type = resultsSet.getString("product_type"); String
		 * region_code = resultsSet.getString("region_range");
		 * 
		 * Task task = new Task(taskId, production_plan_name, product_type,
		 * region_code);//创建一个Task
		 * createdJob(task);//解析task中的region_code信息，生成jobsName
		 * tasksQueue.add(task);//把task放入队列中
		 */ }

}
